package com.kaikeba.controller;

import com.kaikeba.bean.*;
import com.kaikeba.mvc.ResponseBody;
import com.kaikeba.service.CourierService;
import com.kaikeba.service.impl.CourierServiceImpl;
import com.kaikeba.util.FormatUtil;
import com.kaikeba.util.JsonUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CourierController {

    private CourierService service = new CourierServiceImpl();

    @ResponseBody("/courier/console.do")
    public String console(HttpServletRequest req, HttpServletResponse resp){
        Map<String, Integer> console = service.console();
        String s = JsonUtil.toJson(console);
        System.out.println(s);
        return s;
    }

    @ResponseBody("/courier/list.do")
    public String list(HttpServletRequest req, HttpServletResponse resp){
        ResultData<BootstrpCourier> data = new ResultData();
        int offset = Integer.parseInt(req.getParameter("offset"));
        int pageNumber = Integer.parseInt(req.getParameter("pageNumber"));
        //数据
        List<Courier> all = service.findAll(true, offset, pageNumber);
        List<BootstrpCourier> bootstrpCourier = new ArrayList<>();
        for (Courier courier : all) {
            int id = courier.getId();
            String userName = courier.getUserName();
            String userPhone = courier.getUserPhone();
            String identityNumber = courier.getIdentityNumber();
            String password ="*****";
            int passNumber = courier.getPassNumber();
            String loginTime = courier.getLoginTime()!=null? FormatUtil.format(courier.getLoginTime()):"-";
            String resgisterTime = courier.getRegisterTime()!=null?FormatUtil.format(courier.getRegisterTime()):"-";

            BootstrpCourier b = new BootstrpCourier(id,userName,userPhone,identityNumber,passNumber,password,resgisterTime,loginTime);
            bootstrpCourier.add(b);
        }

        data.setRows(bootstrpCourier);
        //总人数
        Map<String, Integer> console = service.console();
        Integer sum = console.get("c_sum");
        data.setTotal(sum);
        String s = JsonUtil.toJson(data);
        System.out.println(s);
        return s;
    }

    @ResponseBody("/courier/add.do")
    public String add(HttpServletRequest req, HttpServletResponse resp){
        String userName = req.getParameter("userName");
        String userPhone = req.getParameter("userPhone");
        String identityNumber = req.getParameter("identityNumber");
        String password = req.getParameter("password");
        Courier courier = new Courier(userName,userPhone,identityNumber,password);
        boolean insert = service.insert(courier);
        Message message = new Message();
        if (insert){
            message.setStatus(0);
            message.setResult("添加成功");
        }else {
            message.setStatus(-1);
            message.setResult("添加失败");
        }
        String s = JsonUtil.toJson(message);
        return s;
    }

    @ResponseBody("/courier/find.do")
    public String find(HttpServletRequest req, HttpServletResponse resp){
        String phone = req.getParameter("phone");
        List<Courier> courierList = service.findByCourierPhone(phone);
        Message message = new Message();
        if (!courierList.isEmpty()){
            Courier courier = courierList.get(0);
            message.setStatus(0);
            message.setResult("查找成功");
            message.setData(courier);
        }else {
            message.setStatus(-1);
            message.setResult("抱歉，暂无此电话号码");
        }
        String s = JsonUtil.toJson(message);
        System.out.println(s);
        return s;
    }


    @ResponseBody("/courier/update.do")
    public String update(HttpServletRequest req, HttpServletResponse resp){
        int id = Integer.parseInt(req.getParameter("id"));
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String identityNumber = req.getParameter("identityNumber");
        String password = req.getParameter("password");

        Courier courier = new Courier(name,phone,identityNumber,password);
        boolean update = service.update(id, courier);
        Message message = new Message();
        if (update){
            message.setStatus(0);
            message.setResult("用户更新成功");
        }else {
            message.setStatus(-1);
            message.setResult("用户更新失败");
        }

        String s = JsonUtil.toJson(message);
        return s;
    }


    @ResponseBody("/courier/delete.do")
    public String delete(HttpServletRequest req, HttpServletResponse resp){
        int id = Integer.parseInt(req.getParameter("id"));
        boolean delete = service.delete(id);
        Message message = new Message();
        if (delete){
            message.setStatus(0);
            message.setResult("删除成功");
        }else {
            message.setStatus(-1);
            message.setResult("删除失败");
        }
        String s = JsonUtil.toJson(message);
        return s;
    }
}
